package qj.util;

import qj.tool.string.StringTracker;

public class JavaCode1_5Util extends JavaCodeUtil {
   /**
    *
    * @param source
    * @return content with no comment
    */
   public static String rejectAllComment(CharSequence source) {
       StringBuffer code;
       if (source instanceof StringBuffer)
           code = (StringBuffer) source;
       else
           code = new StringBuffer(source.toString());
       
       int length = code.length();
       StringTracker charTracker = new StringTracker(new String[] {"//", "/*", "@"});
       // Find // and /*
       // Find fail: break
       // What find first => check in string
       // if fail: rollIndex + 2, continue
       // true: replace with spaces
       int posTemp;
       for (int rollIndex = 0;rollIndex < length;rollIndex++) {
       	
           if (charTracker.track(code.charAt(rollIndex))
           		&& !posInString(rollIndex, code)
           		) {
               if (charTracker.checkAgainst("//")) {
                   posTemp = code.indexOf("\n", rollIndex + 1);
                   if (posTemp==-1) posTemp = length;
                   replaceWords(rollIndex - 1, posTemp, code);
               } else if (charTracker.checkAgainst("/*")) {
                   posTemp = code.indexOf("*/", rollIndex + 1) + 2;
                   if (posTemp==1) posTemp = length;
                   replaceWords(rollIndex - 1, posTemp, code);
               } else {
                   posTemp = StringUtil4.findBefore('(', '\n', code, rollIndex + 1);
                   if (posTemp == -1) {
                	   posTemp = code.indexOf("\n", rollIndex + 1);
                	   if (posTemp==-1) posTemp = length;
                   } else {
                	   posTemp = StringUtil4.findNext(")", "(", code, posTemp + 1) + 1;
                	   if (posTemp==0) posTemp = length;
                   }
                   replaceWords(rollIndex, posTemp, code);
               }
               charTracker.refresh();
               rollIndex = posTemp;
           }
       }
       return code.toString();
   }
}
